<?php

class VoteController extends BaseController {
        
    public function indexAction($requestData) {
		$this->data['event'] = p1_event::get($requestData['arg0'], 'crc32(id)');
		$this->data['event']->details = p1_event_detail::get($this->data['event']->id, "event");
		
		if(isset($requestData['login'])) {
			BaseController::forward("Vote", "verify");
			VoteController::verifyAction($requestData);
		}
		
		if(isset($requestData['submit'])) {
			BaseController::forward("Vote", "verify");
			VoteController::verifyAction($requestData);
		}
		
		$this->data['pageTitle'] = 'Vote for ' . $this->data['event']->name . ' - Peak 100';
		
		$this->getArray(array(10, 8, 5, 4, 3), $this->data);
		
		$this->data['events'] = p1_event::getAll();
    }
    
    public function verifyAction($requestData) {
    	if(!isset($requestData['state'])) {
    		$this->data['state'] = base64_encode(serialize($requestData));
    	} 
    	if(isset($requestData['login'])) {
    		$user = pk_user::get($requestData['email'], "email");
    		if($user->exists() && $user->verified && $user->enabled) {
    			$passwordVerification = crypt($requestData['password'], sprintf("$1$%s$", substr($user->salt, 0, 8)));
    			if($user->password == $passwordVerification) {
	    			$user->login();
	    			
	    			setcookie("p1c_name", $user->username, time() + (2*24*60*60), "/", str_replace('www', '', $_SERVER['SERVER_NAME']));
	    			
    			} else {
    				$this->data['errors'] = array("Wrong email and/or password");
    			}
    		} else {
    			$this->data['errors'] = array("Wrong email and/or password");
    		}
    	}
    	if(pk_user::isLoggedIn()) {
			$vote = new p1_vote($requestData);
			$vote->user = pk_user::id();
			$vote->event = $this->data['event']->id;
			$vote->calculated = '0';
			if($vote->isAlreadyDone()) {
				BaseController::forward("Vote", "already_done");
				VoteController::already_doneAction($requestData);
				return;
			}
			if(isset($requestData['state'])) {
				$data = unserialize(base64_decode($requestData['state']));
				$vote->distance = $data['distance'];
				$vote->time_to_complete = $data['time_to_complete'];
				$vote->terrain = $data['terrain'];
				$vote->climate = $data['climate'];
				$vote->factor = $data['factor'];
				$vote->support = $data['support'];
			}
			$vote->save();
			#log user action
			BaseController::redirect('../vote/thank_you/'. sprintf("%u", crc32($this->data['event']->id)) . "/");
    	}
    }
    
    public function thank_youAction($requestData) {
    	$this->data['event'] = p1_event::get($requestData['arg0'], 'crc32(id)');
    	$this->data['events'] = p1_event::getAll();
    	header("refresh: 3;url=". REAL_ROOT . "ranking/");
    }
    
    public function already_doneAction($requestData) {
    	header("refresh: 3;url=". REAL_ROOT . "ranking/");
    }
    
    private function getArray($counts, &$data) {
    	foreach($counts as $count) {
    		$data['array'. $count] = array_keys(array_fill(1, $count, 1));
    	}
    }

}

?>